package org.cloud.automatic.queue.listener;

import lombok.extern.slf4j.Slf4j;
import org.cloud.automatic.queue.entity.SendQueueWorkerBO;
import org.cloud.automatic.queue.service.SendQueueDealService;
import org.cloud.automatic.util.SpringUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @Description: 事件监听
 * @Author: gangzhenga
 * @Date: 2021/7/28 11:31
 */
@Slf4j
@Component
public class TransactionEventComment {
	
    /**
     * @param sendQueueWorkerBO
     * @Description 事件监听执行事务补偿。必须异步执行此方法，否则补偿必然失败。
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handle(SendQueueWorkerBO sendQueueWorkerBO) {
        log.info("===当前事务提交成功，开始远程接口调用事务补偿===");
        try {
            Thread.currentThread().sleep(2000);
            SendQueueDealService service = SpringUtil.getBean(SendQueueDealService.class);
            service.indepDealWork(sendQueueWorkerBO);
        } catch (Exception e) {
            log.error("===事务补偿失败，等待自动任务补偿===", e);
        }
    }
}


